package com.miao.util.collection;

import org.apache.commons.lang.StringUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Created by miao on 2015/4/9.
 */
public class ListUtils {
    public static List sortList(List<Map<String, Object>> list, final String key, final String direction) {
        Collections.sort(list, new Comparator<Map>() {
            @Override
            public int compare(Map o1, Map o2) {
                //"-":降序排列
                String score1 = o1.containsKey(key) ? (String.valueOf(o1.get(key))) : "0";
                String score2 = o2.containsKey(key) ? (String.valueOf(o2.get(key))) : "0";
                if (StringUtils.equalsIgnoreCase(direction, "desc")) {
                    return -(score1.compareTo(score2));
                } else {
                    return (score1.compareTo(score2));
                }
            }
        });
        return list;
    }
}
